package patrol.mobile.com.mobilepatrol.utils;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import patrol.mobile.com.mobilepatrol.bean.LineChartBean;
import patrol.mobile.com.mobilepatrol.bean.NewLineChartBean;

/**
 * Created by z on 2018/9/9.
 * Des:LineChart工具 升级版  可设置多条线 自定义X轴
 */

public class NewLineChartUtil {
    /**
     * 展示曲线
     *
     * @param dataList 数据集合
     * @param strings  X轴
     */
    public static void setLineChart(LineChart lineChart, List<NewLineChartBean> dataList, List<String> strings) {
        lineChart.setDrawGridBackground(false);
        lineChart.setDrawBorders(false);
        lineChart.setDragEnabled(false);
        lineChart.setTouchEnabled(true);
        lineChart.animateY(2500);
        lineChart.animateX(1500);


        /**
         * X轴的设置
         **/
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        /**
         * Y轴的设置
         **/
        YAxis leftYAxis = lineChart.getAxisLeft();
        leftYAxis.setAxisMinimum(0f);
        leftYAxis.setDrawGridLines(false);
        Legend legend = lineChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(12f);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);

        showLineChart(lineChart, dataList, xAxis, strings);
    }


    /**
     * 曲线初始化设置 一个LineDataSet 代表一条曲线
     *
     * @param lineDataSet 线条
     * @param color       线条颜色
     * @param mode
     */
    private static void initLineDataSet(LineDataSet lineDataSet, int color, LineDataSet.Mode mode) {
        lineDataSet.setColor(color);
        lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(3f);
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setValueTextSize(10f);
        //设置折线图填充
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormSize(15.f);
        if (mode == null) {
            //设置曲线展示为圆滑曲线（如果不设置则默认折线）
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        } else {
            lineDataSet.setMode(mode);
        }
    }


    /**
     * 展示曲线
     *
     * @param dataList 数据集合
     * @param strings  X轴
     */
    private static void showLineChart(LineChart lineChart, final List<NewLineChartBean> dataList, XAxis xAxis, final List<String> strings) {

        for (int i = 0; i < dataList.size(); i++) {
            List<Entry> entries = new ArrayList<>();
            for (int k = 0; k < dataList.get(i).getYBataBeanList().size(); k++) {
                NewLineChartBean data = dataList.get(i);
                Entry entry = new Entry(k, (float) data.getYBataBeanList().get(k).getValue());
                entries.add(entry);
            }
            LineDataSet lineDataSet = new LineDataSet(entries, dataList.get(i).getLineName());
            int ranColor = 0xff000000 | new Random().nextInt(0x00ffffff);
            initLineDataSet(lineDataSet, ranColor, LineDataSet.Mode.LINEAR);
            LineData lineData = new LineData(lineDataSet);
            if (i == 0) {
                lineChart.setData(lineData);
            } else {
                lineChart.getLineData().addDataSet(lineDataSet);
                lineChart.invalidate();
            }
        }

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return strings.get((int) value % strings.size());
            }
        });
    }
}
